#include "window.h"

Window::Window(void) {
	main_widget = new Main_Widget();
	main_widget->hide();
	ui.setupUi(this);
	create_connections();
	init();
}

Window::~Window(void) {
	delete main_widget;
}

void Window::open(void) {
	QString filename = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
	
	
	if (!filename.isEmpty()) {
		QFile qfile(filename);
		if(!qfile.open(QIODevice::ReadOnly)) {
			QMessageBox::information(this, tr("Sirat"),tr("Cannot load %1.").arg(filename));
			return;
		}
		else {
			statusBar()->showMessage(tr("%1 opened").arg(filename));
			emit open_file(filename);
			main_widget->show();
		}
	}
}

void Window::change_statusbar(QString _message) {
	statusBar()->showMessage(_message);
}

void Window::init(void) {
	showMaximized();
	setCentralWidget(main_widget);
	setWindowTitle(tr("Sirat v0.1"));
	statusBar()->showMessage(tr("Ready"));
}

void Window::create_connections(void) {
	connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close()));
	connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(open()));
	connect(ui.actionClose,SIGNAL(triggered()), main_widget,SLOT(file_closed()));
	connect(this,SIGNAL(open_file(QString)), main_widget,SLOT(file_opened(QString)));
	connect(main_widget,SIGNAL(statusbar_message(QString)),this,SLOT(change_statusbar(QString)));
}


